package com.tarena.lbs.user.controller;


import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.param.UserTagsParam;
import com.tarena.lbs.user.pojo.po.UserPO;

import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.LoginService;
import com.tarena.lbs.user.service.UserService;
import com.tarena.lbs.user.service.UserTagsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 手机小程序用户核心接口实现
 */
@Slf4j
@Tag(name = "用户接口")
@RestController
public class UserController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserTagsService userTagsService;

    /**
     * 用户登录接口（修复参数解析问题）
     */
    @Operation(summary = "用户登录")
    @PostMapping("/passport/user/login")
    public Result<LoginVO> userLogin(@Validated @RequestBody UserLoginParam param) throws BusinessException {
        // 打印接收的参数，确认是否有值
        log.debug("登录接口接收参数: phone={}, password={}", param.getPhone(), param.getPassword());

        // 手动校验参数是否为空（提前拦截）
        if (param.getPhone() == null || param.getPhone().trim().isEmpty()) {
            throw new BusinessException("-2", "手机号不能为空");
        }
        if (param.getPassword() == null || param.getPassword().trim().isEmpty()) {
            throw new BusinessException("-2", "密码不能为空");
        }

        // 调用登录服务
        LoginVO loginVO = loginService.userLogin(param);
        log.debug("登录成功，出参: {}", loginVO);
        return new Result<>(loginVO);
    }

    // 其他接口保持不变...
    @Operation(summary = "用户注册")
    @PostMapping("/basic/user/info/register")
    public Result<Void> register(@RequestBody UserParam param) throws BusinessException {
        log.debug("用户注册请求，入参: {}", param.getProviderType());
        userService.register(param);
        log.debug("用户注册成功，入参: {}", param);
        return Result.success();
    }

    @Operation(summary = "用户详情")
    @PostMapping("/basic/user/detail/{id}")
    public Result<UserVO> detail(@PathVariable Long id)throws BusinessException{
        log.debug("查询登录用户详情");
        UserVO detail = userService.getById(id);
        //通过用户id查询登录详情
        log.debug("查询登录用户详情成功，出参:{}",detail);
        return new Result<>(detail);
    }

    @Operation(summary = "用户标签关联")
    @PostMapping("/basic/user/tag")
    public Result<Void> bindUserTags(@Validated @RequestBody UserTagsParam param) throws BusinessException {
        log.info("用户标签关联请求，入参: {}", param);
        userTagsService.bindUserTags(param);
        log.info("用户标签关联成功，入参: {}", param);
        return Result.success();
    }
    @Operation(summary = "查询用户列表")
    @PostMapping("/basic/user/list")
    public Result<List<UserPO>> list(@RequestBody UserParam  param)throws BusinessException{
        List<UserPO> list=userService.list(param);
        return new Result<>(list);
    }
    @Operation(summary = "删除用户")
    @PostMapping("/basic/user/delete/{id}")
    public Result<Void> delete(@PathVariable Integer id)throws BusinessException{
        log.info("删除用户{}",id);
        userService.delete(id);
        return Result.success();
    }
}